package cn.community.care.module.system.service.illnesshistory;

import cn.community.care.module.system.controller.admin.illnesshistory.vo.IllnessHistoryPageReqVO;
import cn.community.care.module.system.controller.admin.illnesshistory.vo.IllnessHistorySaveReqVO;
import cn.community.care.module.system.dal.dataobject.illnesshistory.IllnessHistoryDO;
import cn.community.care.module.system.dal.mysql.illnesshistory.IllnessHistoryMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;


import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 用户生病历史 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IllnessHistoryServiceImpl implements IllnessHistoryService {

    @Resource
    private IllnessHistoryMapper illnessHistoryMapper;

    @Override
    public Long createIllnessHistory(IllnessHistorySaveReqVO createReqVO) {
        // 插入
        IllnessHistoryDO illnessHistory = BeanUtils.toBean(createReqVO, IllnessHistoryDO.class);
        illnessHistoryMapper.insert(illnessHistory);
        // 返回
        return illnessHistory.getId();
    }

    @Override
    public void updateIllnessHistory(IllnessHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateIllnessHistoryExists(updateReqVO.getId());
        // 更新
        IllnessHistoryDO updateObj = BeanUtils.toBean(updateReqVO, IllnessHistoryDO.class);
        illnessHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteIllnessHistory(Long id) {
        // 校验存在
        validateIllnessHistoryExists(id);
        // 删除
        illnessHistoryMapper.deleteById(id);
    }

    private void validateIllnessHistoryExists(Long id) {
        if (illnessHistoryMapper.selectById(id) == null) {
//            throw exception(ILLNESS_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public IllnessHistoryDO getIllnessHistory(Long id) {
        return illnessHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<IllnessHistoryDO> getIllnessHistoryPage(IllnessHistoryPageReqVO pageReqVO) {
        return illnessHistoryMapper.selectPage(pageReqVO);
    }

    @Override
    public IllnessHistoryDO getIllnessHistoryByUserId(Long id) {
        return illnessHistoryMapper.selectOne(IllnessHistoryDO::getUserId, id);
    }

}